package com.qiangqiang.resoldhouse.util;

import com.alibaba.fastjson.JSON;

public class JsonUtil {

    /**
      * @author sky
      * @date 2018/8/9
      * @param [value]
      * @return java.lang.String
      * @describe: Bean 转  String
     */
    public static  <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        }
        return JSON.toJSONString(value);
    }

    /**
      * @author sky
      * @date 2018/8/9
      * @param [str, clazz]
      * @return T
      * @describe: String 转 Bean
     */
    public static <T> T stringTobean(String str, Class<T> clazz) {
        if (str == null || str.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (clazz == String.class) {
            return (T) str;
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(str);
        } else {
            return JSON.toJavaObject(JSON.parseObject(str), clazz);
        }
    }

}
